student_list = [
    {'no': 1, 'name': '小明', 'age': 13, 'sex': 'male'},
    {'no': 2, 'name': '小红', 'age': 12, 'sex': 'female'},
    {'no': 2, 'name': '小红', 'age': 12, 'sex': 'female'},
]

def print_welcom_info():
    print("""
    1-所有学生信息
    2-修改学生信息
    3-添加学生信息
    4-删除学生信息
    0-退出程序
    """)

def get_user_input():
    select_num = input('选择操作编号：')
    if select_num in ['1', '2', '3', '4', '0']:
        return int(select_num)
    else:
        print('输入不合法请重新选择')
        return None

def get_stu_list():
    """
    打印学生列表
    :return:
    """
    print('{:<8}{:<8}{:<8}{:<8}{:<8}'.format('索引', '学号', '姓名', '年龄', '性别'))
    for index, stu in enumerate(student_list):
        # index → 0 ；stu → {'no': 1, 'name': '小明', 'age': 13, 'sex': 'male'}
        no = stu['no']
        name = stu['name']
        age = stu['age']
        sex = stu['sex']
        print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(index, no, name, age, sex))
    print('学生列表打印完成')
    print('='*50)

def update_stu_by_id():
    print('修改>根据学生索引修改')
    get_stu_list()
    sub_select_num = input('你想修改哪个学生，键入他的[索引]:')
    if sub_select_num.isdigit() and int(sub_select_num) in range(0, len(student_list)):
        new_name = input('修改后姓名：')
        new_age = int(input('修改后的年龄：'))
        new_sex = input('修改后的性别：')
         # {'no': 1, 'name': '小明', 'age': 13, 'sex': 'male'}
        student_list[int(sub_select_num)]['name'] = new_name
        student_list[int(sub_select_num)]['age'] = new_age
        student_list[int(sub_select_num)]['sex'] = new_sex
        print('修改成功')
        print('='*50)
    else:
        print('输入的索引不存在或输入非法字符')

def add_stu():
    print('添加>输入信息添加学生')
    new_no = input('请输入学生学号：')
    new_name = input('请输入学生姓名：')
    new_age = int(input('请输入学生年龄：'))
    new_sex = input('请输入学生性别')

    new_stu = {}
    new_stu['no'] = new_no
    new_stu['name'] = new_name
    new_stu['age'] = new_age
    new_stu['sex'] = new_sex

    student_list.append(new_stu)
    print('添加完成')
    print('='*50)

def delete_stu_by_id():
    print('删除>根据学生索引删除')
    get_stu_list()
    sub_select_num = input('你想删除哪个学生，键入[索引]:')
    if sub_select_num.isdigit() and int(sub_select_num) in range(0, len(student_list)):
        student_list.pop(int(sub_select_num))
        print('删除成功')
        print('='*50)
    else:
        print('用户输入的索引不存在或输入不合法')

while True:
    # 打印欢迎信息
    print_welcom_info()
    # 接收用户选择
    select_num = get_user_input()
    if not select_num:
        continue
    else:
        if select_num == 1:
            get_stu_list()
        elif select_num == 2:
            update_stu_by_id()
        elif select_num == 3:
            add_stu()
        elif select_num == 4:
            delete_stu_by_id()
        else:
            break




student_list = [{'name': '小明', 'age':10, 'sex':'male'},
            {'name':'小红', 'age':12, 'sex':'female'},
            {'name':'小李', 'age':12, 'sex':'male'}
            ]




def panduan1():
    print('添加>输入信息添加学生')
    new_no = input('请输入学生学号：')
    new_name = input('请输入学生姓名：')
    new_age = int(input('请输入学生年龄：'))
    new_sex = input('请输入学生性别')
    new_stu = {}
    new_stu['no'] = new_no
    new_stu['name'] = new_name
    new_stu['age'] = new_age
    new_stu['sex'] = new_sex
    student_list.append(new_stu)
    print('添加成功!')
def panduan2():
    print('{:<10}{:<10}{:<10}'.format('姓名','年龄','性别'))
    print('-'*50)
    for i in range(0,len(student_list)):
        num=student_list[i]
        name=num['name']
        age=num['age']
        sex=num['sex']
        print('{:<10}{:<10}{:<10}'.format(name,age,sex))
        print('~'*50)




def panduan():
    while True:
        print("""欢迎使用小金系统
            1-添加学生姓名
            2-查询所有
            0-退出
        """)
        num=int(input('请输入你要查找的序号:'))
        if num==1:
            panduan1()
        if num==2:
            panduan2()
        if num==0:
            break
panduan()









